package cn.itcast.p2p20.fragments;


import android.support.v4.app.Fragment;
import android.view.View;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.itcast.p2p20.R;
import cn.itcast.p2p20.bean.ProfitData;
import cn.itcast.p2p20.bean.request.QueryRequest;
import cn.itcast.p2p20.bean.response.ProfitResponse;
import cn.itcast.p2p20.net.BaseRequest;
import cn.itcast.p2p20.net.SuccessResultCallback;
import cn.itcast.p2p20.utils.Util;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProfitFragment extends BaseFragment implements SuccessResultCallback<ProfitResponse> {

    @InjectView(R.id.barchart)
    BarChart barchart;

    @Override
    protected void initData() {

        sendRequest(new QueryRequest(mContext, "profit"), ProfitResponse.class, this);
    }

    @Override
    protected void initView(View childFragmentView) {

        ButterKnife.inject(this, childFragmentView);
    }

    @Override
    protected String getTitle() {
        return null;
    }

    @Override
    protected int getContentLayoutRes() {
        return R.layout.fragment_profit;
    }

    @Override
    public void onSuccessResult(BaseRequest request, ProfitResponse response) {

        ProfitData profitData = response.data;
        List<String> xVals = new ArrayList<>();
        List<BarEntry> yVals = new ArrayList<>();
        ArrayList<Integer> colors = new ArrayList<>();
        BarDataSet dataSet = new BarDataSet(yVals, "每日收益");

        List<ProfitData.ProfitBean> profitBeanList = profitData.getProfit();

        for (int i = 0; i < profitBeanList.size(); i++) {
            ProfitData.ProfitBean profitBean = profitBeanList.get(i);
            xVals.add(profitBean.getDate());
            yVals.add(new BarEntry((float) profitBean.getProfit(), i));
            colors.add(Util.generaRandomColor(mContext));
        }


        dataSet.setColors(colors);
        dataSet.setDrawValues(false);
        barchart.animateXY(2000, 2000);
        barchart.setData(new BarData(xVals, dataSet));
    }
}
